package com.kehutong.charge.admin;

import com.kehutong.charge.dto.ChargeStatistDTO;
import com.kehutong.charge.entity.ChargeArrears;
import com.kehutong.charge.entity.ChargeBillInterim;
import com.kehutong.charge.entity.ChargeFeeConfig;
import com.kehutong.charge.service.ChargeArrearsService;
import com.kehutong.charge.service.ChargeBillInterimService;
import com.kehutong.charge.util.CommonClient;
import com.kehutong.charge.util.CommonUtil;
import com.kehutong.common.DeptService;
import com.kehutong.common.PageUtils;
import com.kehutong.common.dto.DeptDTO;
import com.kehutong.common.util.Token;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * @program: kht
 * @description: 临时账单管理
 * @author: joneron
 * @create: 2021/6/23
 **/
@MvcAction
@ReqMapping("/admin/pay/charge/bill/interim")
public class ChargeBillInterimController {

    private static final Logger logger = LoggerFactory.getLogger(ChargeBillInterimController.class);

    @Inject
    DeptService deptService;
    @Inject
    ChargeBillInterimService chargeBillInterimService;



    @ReqMapping("/page")
    @HasPermission("pay:charge:bill:interim:view")
    public Object page(JSONObject jsonObject) throws Exception {
        return chargeBillInterimService.findPage(jsonObject);
    }


    @ReqMapping("/page/export")
    @HasPermission("pay:charge:bill:interim:view")
    public void pageExport(HttpServletRequest req, HttpServletResponse resp, JSONObject jsonObject){
        chargeBillInterimService.pageExport(req,resp,jsonObject);
    }


    /**
     * 临时收费账单导入模板生成
     * @param req
     * @param resp
     */
    @ReqMapping("/template/export")
    @HasPermission("pay:charge:meter:reading:view")
    public void templateExport(HttpServletRequest req, HttpServletResponse resp){
        chargeBillInterimService.templateExport(req,resp);
    }


    @ReqMapping("/list")
    @HasPermission("pay:charge:bill:interim:view")
    public Object list(JSONObject jsonObject) {
        return chargeBillInterimService.findList(jsonObject);
    }


    @Bean(newInstance=true, copy=true)
    @HasPermission("pay:charge:bill:interim:edit")
    @ReqMapping("/save")
    public Object save(ChargeBillInterim entity, Token token) throws Exception {
        List<String> departList = token.getDepartments();
        if(null != departList && departList.size() > 0){
            logger.error("用户{}数据异常，部门为空！",token.getUsername());
            DeptDTO deptDTO = deptService.getDept(departList.get(0));
            entity.setDepartmentNo(deptDTO.getDepartmentNo());
            entity.setDepartmentNos(deptDTO.getDepartmentNos());
            entity.setDepartmentName(deptDTO.getDepartmentName());
        }
        entity.setCreateName(token.getUsername());
        String billNo = CommonUtil.generateRandom("LS");
        entity.setInterimBillNo(billNo);

        chargeBillInterimService.save(entity);
        //返回添加的id
        return Result.success(billNo);
    }


    /**
     * 临时账单列表导入检查
     * @param jsonObject
     */
    @ReqMapping("/import/check")
    public Object importExcelCheck(JSONObject jsonObject){
        chargeBillInterimService.importExcelCheck(jsonObject);
        return Result.success();
    }


    /**
     * 临时账单列表导入
     * @param jsonObject
     * @param token
     * @Author jian Ye
     */
    @ReqMapping("/import")
    public Object importExcel(JSONObject jsonObject, Token token) throws Exception {
        return chargeBillInterimService.importExcel(jsonObject,token);
    }


    @Bean(copy=true)
    @HasPermission("pay:charge:bill:interim:edit")
    @ReqMapping("/update")
    public Object update(ChargeBillInterim entity,Token token) throws Exception {

        entity.setUpdateName(token.getUsername());
        return chargeBillInterimService.update(entity);
    }

    @HasPermission("pay:charge:bill:interim:edit")
    @ReqMapping("/delete")
    public Object delete(ChargeBillInterim entity) {

        return chargeBillInterimService.deleteById(entity.getId());
    }


    @ReqMapping("/get")
    public Object get(ChargeBillInterim entity) {

        return Result.success(entity);
    }


    @ReqMapping("/getObj")
    public Object getObj(JSONObject jsonObject) {

        return Result.success(chargeBillInterimService.getObj(jsonObject));
    }


    /**
     * 统计功能
     */
    @ReqMapping("/statist")
    @HasPermission("pay:charge:bill:interim:edit")
    public Object statist(JSONObject jsonObject,Token token) throws Exception {

        List<ChargeStatistDTO> listObj = chargeBillInterimService.findStatist(jsonObject);

        return PageUtils.buildPage(jsonObject,listObj);
    }


    /**
     * 批量导入
     * @param
     * @return
     */
    @ReqMapping("/batchImport")
    @HasPermission("pay:charge:bill:interim:edit")
    public Object batchImport(JSONObject jsonObject, Token token) {

        return Result.success("暂未实现！");
    }


    /**
     * 批量导出
     * @param
     * @return
     */
    @ReqMapping("/batchExport")
    @HasPermission("pay:charge:bill:interim:edit")
    public Object batchExport(JSONObject jsonObject, Token token) {

        return Result.success("暂未实现！");
    }


    /**
     * 临时账单作废(单条或多条)
     * @param jsonObject
     * @return
     */
    @ReqMapping("/cancelInterimBill")
    @HasPermission("pay:charge:bill:mange:edit")
    public Object cancelInterimBill(JSONObject jsonObject, Token token) {

        return chargeBillInterimService.cancelInterimBill(jsonObject,token);
    }
}
